package com.czl.module_user.viewmodel

import androidx.databinding.ObservableField
import com.czl.base.base.AppManager
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.config.AppConstants
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.UserInfo
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class UserViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    val uc = UiChangeEvent()
    var userName = ObservableField("")
    var avatarUrl = ObservableField("")
    var userPhone = ObservableField("")
    var areaName = ObservableField(model.getAreaName())

    class UiChangeEvent {
        val changeAreaEvent: SingleLiveEvent<Void> = SingleLiveEvent()
    }

    val onOrderClick: BindingCommand<Any> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Order.F_ORDER_MANAGER)
    })

    val onNoticeCommand: BindingCommand<Any> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Notice.F_NOTICE)
    })

    val onNavSettingClick: BindingCommand<Any> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.User.F_SETTING)
    })

    val onRepairClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Report.F_REPAIR_REPORT)
    })

    val onDevelopingClick: BindingCommand<Void> = BindingCommand(BindingAction {
        showNormalToast("功能正在开发中，敬请期待")
    })

    /**
     * 获取用户详细信息
     */
    fun getUserInfo() {
        model.apply {
            getUserInfoNet()
                .compose(RxThreadHelper.rxSchedulerHelper(this@UserViewModel))
                .subscribe(object : ApiSubscriberHelper<BaseBean<UserInfo>>() {
                    override fun onResult(t: BaseBean<UserInfo>) {
                        t.data?.let {
                            saveUserInfoData(it)
                            userName.set(it.nickName)
                            userPhone.set(it.phonenumber)
                            avatarUrl.set(it.avatar)
                        }
                    }

                    override fun onFailed(msg: String?) {
                        showNormalToast(msg)
                    }

                })
        }
    }

}